package com.ruoyi.web.controller.platform;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/platform/saveInterface")
public class saveDataController  extends BaseController {
    @ResponseBody
    @Log(title = "开放式写数据接口", businessType = BusinessType.UPDATE)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody(required = true) Map<String,Object> data)
    {
//           入参
//        {"tableName":"gjgydjjc",
//                "dataType":"Array",
//                "data":[{"PE_VISIT_ID":4,"PE_ID":"125883"},{"other":false,"PE_VISIT_ID":2,"PE_ID":"128544","result":"宫颈液基细胞学检查：无上皮内病变或恶性病变(NILM).\r\r","doctorName":"李思澌"}]
//        }

        String[] tmp ={"cscsjcxx","gjgbs","gjgfkjc","gjghpvjc","gjgsf","gjgxbxjc","gjgydjjc","gjgzhzd","gjgzzxjc","rxabs","rxasf","rxaxx","rxgczxx"};

        String tableName=data.get("tableName").toString();
        String dataType=data.get("dataType").toString();
        boolean flage=false;
        for(String s:tmp){
            if(tableName.equals(s))
            {
                flage=true;
                break;
            }
        }
        if(flage)
        {
            if(dataType.equals("Object"))
            {

                Map<String,Object> savedata=(Map<String,Object>)data.get("data");
                Record saveRecord=MapToRecord(savedata);
                if(saveRecord.get("pe_id")!=null && saveRecord.get("pe_visit_id")!=null)
                {
                    String pe_id=saveRecord.get("PE_ID").toString();
                    String pe_visit_id=saveRecord.get("PE_VISIT_ID").toString();
                    Record vRecord= Db.findFirst("select * from  "+tableName+" where pe_id='"+pe_id+"' and pe_visit_id='"+pe_visit_id+"'");
                    if(vRecord!=null)
                    {
                        return AjaxResult.error("["+tableName+"]表已经存在入参数据，请勿重复写入！");
                    }
                    else
                    {
                        if(Db.save(tableName,"PE_ID,PE_VISIT_ID",saveRecord))
                        {
                            return AjaxResult.success("["+pe_id+"]["+pe_visit_id+"]数据保存成功！");
                        }
                        else
                        {
                            return AjaxResult.error("["+pe_id+"]["+pe_visit_id+"]数据保存失败！");
                        }
                    }
                }
                else
                {
                    return AjaxResult.error("待写入数据缺少[pe_id][pe_visit_id]主键入参！");

                }


            }
            else if(dataType.equals("Array"))
            {
                List<Map<String,Object>> list=(List<Map<String,Object>>) data.get("data");
                String msg="";
                for(Map<String,Object> tmpData:list)
                {
                    Record saveRecord=MapToRecord(tmpData);
                    if(saveRecord.get("PE_ID")!=null && saveRecord.get("PE_VISIT_ID")!=null)
                    {
                        String PE_ID=saveRecord.get("PE_ID").toString();
                        String PE_VISIT_ID=saveRecord.get("PE_VISIT_ID").toString();
                        Record vRecord= Db.findFirst("select * from  "+tableName+" where pe_id='"+PE_ID+"' and pe_visit_id='"+PE_VISIT_ID+"'");
                        if(vRecord!=null)
                        {
                            msg+="["+tableName+"]表["+PE_ID+"]["+PE_VISIT_ID+"]已经存在入参数据，请勿重复写入！\r\n";

                        }
                        else
                        {
                            if(Db.save(tableName,"PE_ID,PE_VISIT_ID",saveRecord))
                            {
                                msg+="["+tableName+"]表["+PE_ID+"]["+PE_VISIT_ID+"]数据保存成功！\r\n";

                            }
                            else
                            {
                                msg+="["+tableName+"]表["+PE_ID+"]["+PE_VISIT_ID+"]数据保存失败！\r\n";
                            }
                        }
                    }
                    else
                    {
                        msg+="["+tableName+"]表待写入数据缺少[pe_id][pe_visit_id]主键入参！\r\n";
                    }
                }
                return AjaxResult.success(msg);
            }
            else
            {
                return AjaxResult.error("["+dataType+"]写入数据类型错误！");
            }
        }
        else
        {
            return AjaxResult.error("["+tableName+"]表不在授权写入范围内");
        }

    }

    public Record MapToRecord(Map<String,Object> data){
        Record returnRecord=new Record();
        String[] exception ={"PE_ID","PE_VISIT_ID","CHECK_DATE","CHECK_PERSON","PE_DATE","DOCTOR_NAME","YEAR_REPORTED","QUARTERLY_REPORTED","result","dept_result","create_time"};
        Map<String,Object> newdata=new HashMap<>();
        for(String field:data.keySet())
        {
            boolean flag=false;
            for(String tmp:exception)
            {
                if(field.equals(tmp))
                {
                    flag=true;
                    break;
                }
            }

            if(flag)
            {
//                newdata.put(field.toLowerCase(),data.get(field));
                newdata.put(field,data.get(field));
            }
        }
        return returnRecord.setColumns(newdata);
    }

}
